package com.Sinewecology.EntropyWechat;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.entrogy.bean.PubCountEntity;
import org.apache.http.ParseException;

import com.Sinewecology.SendMessage.WechatMsgDataBean;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WX_SendMessage {

	private static final Logger logger = LoggerFactory.getLogger(WX_SendMessage.class);

	public static String SendMsg(WechatMsgDataBean msg) throws ParseException, IOException {
		int i;
		String[] keyName = { "first", "keyword1", "keyword2", "keyword3", "keyword4", "keyword5", "keyword6",
				"keyword7", "keyword8", "keyword9" };

		Map<String, TemplateData> param = new HashMap<>();
		List<String> keyList = msg.getKeyword();
		for (i = 0; i < keyList.size() - 1; i++) {
			param.put(keyName[i], new TemplateData(keyList.get(i), "#696969"));
		}
		param.put("remark", new TemplateData(keyList.get(i), "#696969"));
		JSONObject data = WX_UserUtil.packJsonmsg(param);
		JSONObject miniprogram = new JSONObject();
		miniprogram.put("appid", msg.getMiniProgramAppId());
		miniprogram.put("pagepath", msg.getPagepath());
		JSONObject msgJson;
		msgJson = new JSONObject();
		PubCountEntity pubCountEntity = msg.getPubCountEntity();
		if(null != pubCountEntity){
			msgJson.put("touser",pubCountEntity.getOpenid());
		}
		msgJson.put("template_id", msg.getTemplateId());
		msgJson.put("url", msg.getUrl());
		msgJson.put("miniprogram", miniprogram);
		msgJson.put("data", data);
		return WX_TemplateMsgUtil.sendWechatMsgToUser(msgJson, msg);
	}
}
